﻿namespace JxcLite.Shared;

/// <summary>
/// 商业信息挑选框组件。
/// </summary>
public class GoodsPicker : TablePicker<GoodsInfo>
{
    private IBaseDataService Service;

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        Service = await CreateServiceAsync<IBaseDataService>();
        Title = "选择商品";
        Width = 700;
        Table.OnQuery = Service.QueryGoodsesAsync;
        Table.AddColumn(c => c.Code).Width(100);
        Table.AddColumn(c => c.Name, true).Width(120);
        Table.AddColumn(c => c.Category).Width(100);
        Table.AddColumn(c => c.Model).Width(180);
        Table.AddColumn(c => c.Producer).Width(100);
        Table.AddColumn(c => c.Unit).Width(100);
        Table.AddColumn(c => c.Note).Width(200);
    }

    protected override void OnValueChanged(List<GoodsInfo> items)
    {
        var value = SelectType == TableSelectType.Checkbox
                  ? string.Join(",", items.Select(d => d.Code))
                  : items?.FirstOrDefault()?.Code;
        if (ValueChanged.HasDelegate)
            ValueChanged.InvokeAsync(value);
    }
}